(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.facility.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'facilityService' ];
	function updateVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, facilityService) {
		var ctrl = this, facilityInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			var params = {
					facilityId : facilityInfo.facilityId
			};
			facilityService.initTree($scope, ctrl);
			ctrl.limitSelected = function (){
				return false;
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.attributes.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.facility.facilityAttrList';
			ctrl.innerGridOptions.verifyData = 'vm.facilityOld.facilityAttrList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			
			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDPParmList = selectedRows;
			}
			
			authService.updateVerify($scope.$stateParams.params).then(function(response) {
				ctrl.facility = response.data.model.newModel || {};
				ctrl.facilityOld = response.data.model.oldModel || {};
				ctrl.facility.taskId = response.data.task.taskId;
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.facility.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.facility).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.facility.comments = comments;
						authService.updateVerifyReject(ctrl.facility).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedLimitCategory = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.facility.list',
					'state' : 'cda.facility.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.facility.list',
					'state' : 'cda.facility.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ "error appeared: " + angular.toJson(response.error) ]
			});
		}
	}
})(angular);